﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
   public  class TaiKhoanTCDA
    {
       public static TaiKhoanTC GetByID(int maID)
       {
           TaiKhoanTC loai = null;
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_TaiKhoanTC_GetById", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maTKTheChap", maID));
           SqlDataReader dataReader = cmd.ExecuteReader();
           if (dataReader.Read())
           {
               loai = new TaiKhoanTC();
               loai.MaTKTheChap = maID;
               loai.MaTKVay = Convert.ToInt32(dataReader["MaTKVay"]);

               loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
               loai.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
               loai.LoaiHinhTC = Convert.ToString(dataReader["LoaiHinhTC"]);
               loai.TaiKhoanVay = TaiKhoanVayDA.GetByID(loai.MaTKVay);
           }
           cn.Close();
           return loai;
       }
       public static List<TaiKhoanTC> GetByMaTKVay(int maID)
       {
           List<TaiKhoanTC> lst = new List<TaiKhoanTC>();
           TaiKhoanTC loai = null;
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_TaiKhoanTC_GetByMaTKVay", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maTKVay", maID));
           SqlDataReader dataReader = cmd.ExecuteReader();
          while (dataReader.Read())
           {
               loai = new TaiKhoanTC();
               loai.MaTKVay = maID;
               loai.MaTKTheChap = Convert.ToInt32(dataReader["MaTKTheChap"]);

               loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
               loai.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
               loai.LoaiHinhTC = Convert.ToString(dataReader["LoaiHinhTC"]);
               loai.TaiKhoanVay = TaiKhoanVayDA.GetByID(loai.MaTKVay);
               lst.Add(loai);
           }
           cn.Close();
           return lst;
       }
       public static List<TaiKhoanTC> GetByMaLoaiTK(int maID)
       {
           List<TaiKhoanTC> lst = new List<TaiKhoanTC>();
           TaiKhoanTC loai = null;
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_TaiKhoanTC_GetByMaLoaiTK", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maLoaiTK"   , maID));
           SqlDataReader dataReader = cmd.ExecuteReader();
           while (dataReader.Read())
           {
               loai = new TaiKhoanTC();
               loai.MaLoaiTK = maID;
               loai.MaTKTheChap = Convert.ToInt32(dataReader["MaTKTheChap"]);

               loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
               loai.MaTKVay = Convert.ToInt32(dataReader["MaTKVay"]);
               loai.LoaiHinhTC = Convert.ToString(dataReader["LoaiHinhTC"]);
               loai.TaiKhoanVay = TaiKhoanVayDA.GetByID(loai.MaTKVay);
               lst.Add(loai);
           }
           cn.Close();
           return lst;
       }
    }
}
